package com.example.rescuer.zyfypt_926wyr.model;


import com.example.rescuer.zyfypt_926wyr.activities.RegisterActivity;
import com.example.rescuer.zyfypt_926wyr.bean.RegisterBean;
import com.example.rescuer.zyfypt_926wyr.common.Common;
import com.example.rescuer.zyfypt_926wyr.iface.LoginListener;
import com.example.rescuer.zyfypt_926wyr.iface.RegisterListener;
import com.example.rescuer.zyfypt_926wyr.iface.Registeriface;
import com.example.rescuer.zyfypt_926wyr.service.RegisterService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * Created by RESCUER on 2018/5/18.
 */

public class RegisterModel {

    Retrofit retrofit;

    public RegisterModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    public void register(String username, String password, String tel, final RegisterActivity listener) {
        RegisterService service = retrofit.create(RegisterService.class);
        Call<String> call = service.getRegister(username, password, tel);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.body() != null && response.isSuccessful()) {
                    System.out.println("==>>" + "response.isSuccessful()");
                    listener.onResponse(response.body());
                    //使用activity中实现的接口对象，将异步返回的结果，返回到activity中处理
                } else {
                    System.out.println("==>>" + "response.onFail()");
                    listener.onFail("onResponse fail");//使用了java的回调机制
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}


